package service

import (
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model/req"
	"gitee.com/zaiqiang231/lovesport-user-service/app/store"
	"github.com/pkg/errors"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"strconv"
)

type ServiceAuthority struct{}

var ServiceAuthorityInstance = new(ServiceAuthority)

func (authorityService *ServiceAuthority) CreateAuthority(req *model.SysAuthority) error {
	return store.GetDataStore().Authority().Create(req)
}

func (authorityService *ServiceAuthority) DeleteAuthority(req *model.SysAuthority) error {
	var err error
	store.GetDataStore().Authority().Execute(func(db *gorm.DB) {
		//err = db.Preload("Users").First(&req).Error
		//if len(req.Users) != 0 {
		//	err = errors.New("此角色有用户正在使用禁止删除")
		//}
		if !errors.Is(db.Where("parent_id = ?", req.AuthorityId).First(&model.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
			err = errors.New("此角色存在子角色不允许删除")
		}
	})
	if err != nil {
		return err
	}
	store.GetDataStore().Authority().Execute(func(db *gorm.DB) {
		err = db.Select(clause.Associations).Delete(req).Error
	})

	return err
}

func (authorityService *ServiceAuthority) UpdateAuthority(auth *model.SysAuthority) error {
	var err error
	store.GetDataStore().Authority().Execute(func(db *gorm.DB) {
		err = db.Where("authority_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Updates(&auth).Error
	})
	return err
}

func (authorityService *ServiceAuthority) CopyAuthority(req *req.SysAuthorityCopyReq) error {
	var err error
	store.GetDataStore().Authority().Execute(func(db *gorm.DB) {
		var authorityBox model.SysAuthority
		if !errors.Is(db.Where("authority_id = ?", req.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
			err = errors.New("存在相同角色id")
		}
		if err != nil {
			return
		}
		var menus []model.SysMenu
		menus, err = ServiceMenuInstance.GetMenuAuthority(req.OldAuthorityId)
		if err != nil {
			return
		}
		var baseMenu []model.SysBaseMenu
		for _, v := range menus {
			intNum, _ := strconv.Atoi(v.MenuId)
			v.SysBaseMenu.ID = uint(intNum)
			baseMenu = append(baseMenu, v.SysBaseMenu)
		}
		req.Authority.SysBaseMenus = baseMenu
	})

	err = store.GetDataStore().Authority().Create(&req.Authority)

	return err
}

func (authorityService *ServiceAuthority) GetAuthorityList(req req.PageInfo) (interface{}, error) {
	list, err := store.GetDataStore().Authority().List(req)
	if err != nil {
		return nil, err
	}
	for k := range list.List {
		err = authorityService.findChildrenAuthority(&list.List[k])
	}
	return list, err
}

func (authorityService *ServiceAuthority) GetAllAuthorityList() (interface{}, error) {
	list, err := store.GetDataStore().Authority().AllList()
	if err != nil {
		return nil, err
	}
	for k := range list.List {
		err = authorityService.findChildrenAuthority(&list.List[k])
	}
	return list, err
}

func (authorityService *ServiceAuthority) findChildrenAuthority(authority *model.SysAuthority) (err error) {
	list, err := store.GetDataStore().Authority().GetAuthorityListByParentId(authority.AuthorityId)
	authority.Children = list
	if len(authority.Children) > 0 {
		for k := range authority.Children {
			err = authorityService.findChildrenAuthority(&authority.Children[k])
		}
	}
	return err
}
